//
//  CircleProgress.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/16.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "CircleProgress.h"


@interface CircleProgress (){
    CAShapeLayer* _trackLayer;
    CAShapeLayer* _progressLayer;
    UIColor *_color;
    UIColor *_fillColor;
    CADisplayLink *_displayLink;
    CGFloat _endProgress;
}

@end

@implementation CircleProgress

-(instancetype)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth LineColor:(UIColor *)color fillColor:(UIColor *)fillColor andPercent:(CGFloat)percent
{
    self = [super initWithFrame:frame];
    if (self) {
        _lineWidth = lineWidth;
        _color = color;
        _fillColor = fillColor;
        _endProgress  = percent;
//        [self buildLayout];
        [self addSubview:self.desLb];
    }
    return self;
}

-(void)layoutSubviews
{
    float centerX = self.bounds.size.width/2.0;
    float centerY = self.bounds.size.height/2.0;
    //半径
    float radius = (self.bounds.size.width-_lineWidth)/2.0;
    
    //创建贝塞尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
    
    //添加背景圆环
    
    CAShapeLayer *backLayer = [CAShapeLayer layer];
    backLayer.frame = self.bounds;
    backLayer.fillColor =  [[UIColor clearColor] CGColor];
    backLayer.strokeColor  = _color.CGColor;
    backLayer.lineWidth = _lineWidth;
    backLayer.path = [path CGPath];
    backLayer.strokeEnd = 1;
    [self.layer addSublayer:backLayer];
    
    //创建进度layer
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    //指定path的渲染颜色
    
    _progressLayer.strokeColor  = _color.CGColor;
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = _lineWidth;
    _progressLayer.path = [path CGPath];
    _progressLayer.strokeEnd = 0;

    CALayer *imLayer = [CALayer layer];
    imLayer.frame = self.bounds;
//    UIImage *image = [UIImage imageNamed:@"saber"];
//    imLayer.contents = (__bridge id _Nullable)(image.CGImage);
    imLayer.backgroundColor = _fillColor.CGColor;
    [imLayer setMask:_progressLayer];
    [self.layer addSublayer:imLayer];

    [self setProgress:_endProgress];
    [_desLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(40, self.height/2));
    }];
}

-(void)setProgress:(float)progress
{
    _endProgress = progress;
    _progress = 0;
    _progressLayer.strokeEnd = 0;

    if (!_displayLink) {
          _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateAnimation)];
          _displayLink.paused = NO;
    }
    if (_endProgress==-1) {
        _progressLayer.strokeEnd = _endProgress;
        _displayLink.paused = YES;
        [_displayLink invalidate];
        _displayLink = nil;
    }else{
        _desLb.text = [NSString stringWithFormat:@"%.f%@",0.0,@"%"];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    });
//    NSInteger countP = arc4random()%100*1.0/100;

}
- (void)updateAnimation{
    if (_progress>=_endProgress) {
        _progressLayer.strokeEnd = _endProgress;
        _displayLink.paused = YES;
        _desLb.text = [NSString stringWithFormat:@"%.f%@",_endProgress*100,@"%"];
        [_displayLink invalidate];
        _displayLink = nil;
    }else{
        _progress += arc4random()%10*1.0/100;
        _progressLayer.strokeEnd = _progress;
        _desLb.text = [NSString stringWithFormat:@"%.f%@",_progress*100,@"%"];
    }
}
- (UILabel *)desLb{
    if (!_desLb) {
        _desLb = [UILabel new];
        _desLb.textColor = _fillColor;
        _desLb.font = [UIFont systemFontOfSize:12];
        _desLb.textAlignment = NSTextAlignmentCenter;
    }
    return _desLb;
}
@end
